package zdh.jfinabbsapp.api;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.utils.KJLoger;

import java.util.ArrayList;
import java.util.List;

import zdh.jfinabbsapp.api.API;
import zdh.jfinabbsapp.api.dao.Section;

/**
 * 作者：zdh1000
 * 创建时间： 2015/6/30 21:15
 * 邮箱：wdzdh1000@163.com
 */
public class SectionAPI {

    public static List<Section> getAllSections(String json) {
        List<Section> sectionList = new ArrayList<>();

        try {
            KJLoger.debug(SectionAPI.class.getName()+" : "+json);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray array = jsonObject.getJSONArray("detail");
            Section section = new Section();
            JSONObject temp;
            //添加首页
            sectionList.add(section);
            //依次添加栏目
            for (int i = 0; i < array.length(); i++) {
                temp = array.getJSONObject(i);
                section = new Section();
                section.setId(temp.getInt("id"));
                section.setDisplayIndex(temp.getInt("display_index"));
                section.setName(temp.getString("name"));
                section.setTab(temp.getString("tab"));
                section.setShowStatus(temp.getInt("show_status"));
                sectionList.add(section);
            }
        } catch (JSONException e) {
            KJLoger.debug(SectionAPI.class.getName()+" : "+e.getMessage());
        }
        KJLoger.debug(SectionAPI.class.getName() + " : " + sectionList.size());
        return sectionList;
    }


    public static String[] getAllSections(List<Section> list) {
        String[] sections = new String[0];
        if (!list.isEmpty()) {
            sections = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                sections[i] = list.get(i).getName();
            }
        }
        return sections;
    }

    public static String getAllSectionsUrl() {
        return API.ROOT + "/section";
    }
}
